using System;
using System.Collections.Generic;
using System.Text;
using TXLooker.Common;
using Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Core;
using System.Reflection;
using TXLooker.WorkItemExtention.Core;
using stdole;
using TXLooker.WorkItemExtention.Properties;
using TXLooker.Common.Outlook;

namespace TXLooker.WorkItemExtention.Core
{
    class StateButton
    {
        private CommandBarButton m_button;
        private IOlWorkItemProcessor m_stateChange = null;

        public IPictureDisp Picture
        {
            get { return this.m_button.Picture; }
            set { this.m_button.Picture = value; }
        }

        public IPictureDisp Mask
        {
            get { return this.m_button.Mask; }
            set { this.m_button.Mask = value; }
        }

        public MsoButtonStyle Style
        {
            get { return this.m_button.Style; }
            set { this.m_button.Style = value; }
        }

        protected IOlWorkItemProcessor StateChange
        {
            get { return m_stateChange; }
            set
            {
                if (object.ReferenceEquals(value, null))
                    throw new NullReferenceException("Value cannot be null");
                m_stateChange = value;
            }
        }
      
        protected StateButton()
        { }
        protected StateButton(IOlWorkItemProcessor stateChange)
            : this()
        {
            this.StateChange = stateChange;
        }

        public StateButton(IOlWorkItemProcessor stateChange,CommandBarPopup parent, string name, string toolTip):this(stateChange)
        {
            m_button = (CommandBarButton)parent.Controls.Add(MsoControlType.msoControlButton,Missing.Value,Missing.Value,Missing.Value,true);
            m_button.Caption = name;
            //m_button.Picture = AxHost2.GetIPictureDispFromPicture(Resources.change_state);
            //m_button.Mask = AxHost2.GetIPictureDispFromPicture(Resources.change_state_black);
            m_button.TooltipText = toolTip;
            m_button.Visible = false;
            m_button.Click += new _CommandBarButtonEvents_ClickEventHandler(OnClick);
        }

        void OnClick(CommandBarButton Ctrl, ref bool CancelDefault)
        {
            this.StateChange.ChangeWorkItemsState(this.Name);   
        }

        public string ToolTip
        {
            get { return m_button.TooltipText; }
            set { m_button.TooltipText = value; }
        }

        public bool Visible
        {
            get { return m_button.Visible; }
            set { m_button.Visible = value; }
        }

        public string Name
        {
            get { return m_button.Caption; }
            set { m_button.Caption = value; }
        }

        public void RemoveButton()
        {
            try
            {

                this.m_button.Delete(true);
            }
            catch(Exception ex)
            {
                LogManager.WriteMessageToCustomLog(ex);
            }
        }
    }
}
